

Imports System.Drawing

Namespace NSE

    Public Class StockQuotes
        Implements IStockQuoteProvider



        Public Function GetStockQuote(ByVal stockSymbol As String, Optional ByVal stockExchange As IndiaExchange = 0) As StockQuoteData Implements IStockQuoteProvider.GetStockQuote

            'Anyway this class will return only the NSE data, so this parameter is of no use. Just setting it to the NSE value just to make sure that any future code does not have any problems
            stockExchange = IndiaExchange.NSE

            Dim RetVal As New StockQuoteData
            Dim PageData As String
            'Currently the NSE seems to only use the Key parameter and any value in the Symbol is ignored. But to be on the safe side I am sending both the value and setting them to be the same
            Dim URL As String = "http://www.nseindia.com/marketinfo/equities/cmquote_tab.jsp?key={0}EQN&symbol={0}&flag=0"

            URL = String.Format(URL, stockSymbol)
            PageData = GetURLString(URL)

            If PageData.IndexOf("No records found for selected security") > 0 Then
                ' We got a wrong symbol, throw the Invalid Symbol Exception
                Dim ex As New InvalidSymbolException
                ex.Data.Add("Symbol", stockSymbol)
                Throw ex
            End If
            'The NSE Site shows a page when they are down or unable to process the request
            If PageData.IndexOf("Technical Difficulties") > 0 Then
                ' We got a wrong symbol, throw the Invalid Symbol Exception
                Dim ex As New DataConnectionException
                ex.Data.Add("Symbol", stockSymbol)
                Throw ex
            End If

            Dim LastPriceBegin As Integer, LastPriceEnd As Integer, LastPrice As Decimal

            LastPriceBegin = PageData.IndexOf("Last Price</b></th><td class=t1><b>") + 35
            LastPriceEnd = PageData.IndexOf("<", LastPriceBegin)
            LastPrice = Decimal.Parse(PageData.Substring(LastPriceBegin, LastPriceEnd - LastPriceBegin))

            Dim ChangeValueBegin As Integer, ChangeValueEnd As Integer, ChangeValue As Decimal
            ChangeValueBegin = PageData.IndexOf("Change</th><td class=t1>") + 24
            ChangeValueEnd = PageData.IndexOf("<", ChangeValueBegin)
            ChangeValue = Decimal.Parse(PageData.Substring(ChangeValueBegin, ChangeValueEnd - ChangeValueBegin))

            Dim TradeDateBegin As Integer, TradeDateEnd As Integer, TradeDate As Date
            TradeDateBegin = PageData.IndexOf("<img src=""/images/trans.gif"" height=""1"" width=""210"" />As on") + 59
            TradeDateEnd = PageData.IndexOf("Hours", TradeDateBegin)
            TradeDate = Date.Parse(PageData.Substring(TradeDateBegin, TradeDateEnd - TradeDateBegin))

            Dim ChangePercentBegin As Integer, ChangePercentEnd As Integer, ChangePercent As Decimal
            ChangePercentBegin = PageData.IndexOf("% Change</th><td class=t1>") + 26
            ChangePercentEnd = PageData.IndexOf("<", ChangePercentBegin)
            ChangePercent = Decimal.Parse(PageData.Substring(ChangePercentBegin, ChangePercentEnd - ChangePercentBegin))

            With RetVal
                .Symbol = stockSymbol
                .StockRate = LastPrice
                .StockChangeValue = ChangeValue
                If ChangeValue < 0 Then
                    .StockChangeDownwards = True
                Else
                    .StockChangeDownwards = False
                End If
                .StockChangePercent = ChangePercent
                .DateOfTrade = TradeDate
            End With

            Return RetVal
        End Function


        Public Function GetStockCharts(ByVal stockSymbol As String, ByVal ChartDuration As GlobalDefnsAndFunctions.ChartDurationType) As System.Drawing.Image Implements IStockQuoteProvider.GetStockCharts
            Throw New NotImplementedException("NSE website does not provide access to images of the Charts")
        End Function

    End Class

End Namespace
